package com.guaju.yueshangclothing.viewmodel;

import android.content.Intent;
import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.view.View;

import com.guaju.yueshangclothing.activity.LoginActivity;
import com.guaju.yueshangclothing.widget.MainBottomItemView;
import com.zhy.autolayout.AutoLinearLayout;

/**
 * Created by guaju on 2018/9/12.
 */

public class MainVM extends BaseObservable {
    //被点击的索引
    public int bottom_index;

    public void onBottomClick(AutoLinearLayout parent, int index) {
        for (int i = 0; i < 4; i++) {
            View v = parent.getChildAt(i);
            if (v instanceof MainBottomItemView) {
                MainBottomItemView bv = (MainBottomItemView) v;
                bv.reset();
            }
        }
        View view = parent.getChildAt(index);
        MainBottomItemView child = null;
        if (view instanceof MainBottomItemView) {
            child = (MainBottomItemView) view;
        }
        child.setChecked(true);
    }

    @Bindable
    public int getIndex() {
        return bottom_index;
    }


    public void onHomeClick(View v) {
        setIndex(0);
        onBottomClick((AutoLinearLayout) v.getParent(), bottom_index);
    }

    public void onCategoryClick(View v) {
        setIndex(1);
        onBottomClick((AutoLinearLayout) v.getParent(), bottom_index);
    }

    public void onCartClick(View v) {
        setIndex(2);
        onBottomClick((AutoLinearLayout) v.getParent(), bottom_index);
    }

    public void onMineClick(View v) {
        setIndex(3);
        onBottomClick((AutoLinearLayout) v.getParent(), bottom_index);
        //登录
        login(v);
    }
    private void login(View v) {
        Intent intent = new Intent(v.getContext(), LoginActivity.class);

        v.getContext().startActivity(intent);
    }

    public void setIndex(int index) {
        this.bottom_index = index;
    }

}
